package com.woniuxy.oa.service.impl;

import com.woniuxy.oa.dao.ApplicationDAO;
import com.woniuxy.oa.dao.EmpDAO;
import com.woniuxy.oa.entity.Application;
import com.woniuxy.oa.entity.Emp;
import com.woniuxy.oa.service.ApplicationService;
import com.woniuxy.oa.util.OaUtil;
import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipInputStream;

/**
 * @author Mr X
 */
@Service
@Transactional
public class ApplicationServiceImpl implements ApplicationService {
    @Autowired
    private ApplicationDAO applicationDAO;
    @Autowired
    private EmpDAO empDAO;

    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private IdentityService identityService;
    @Autowired
    private ProcessEngine processEngine;
    @Autowired
    private HistoryService historyService;

    /**
     * 增加申请
     *
     * @param application
     */
    @Override
    public void add(Application application) throws Exception {
        application.setId(OaUtil.getId());
        application.setTitle("请假申请");
        application.setStatus("1");
        application.setApplicationTime(OaUtil.getNow());
        applicationDAO.add(application);

        //根据zip文件部署流程
        Deployment deployment = repositoryService.createDeployment().addZipInputStream(new ZipInputStream(new FileInputStream(new File("C:\\Users\\Mr X\\Desktop\\蜗牛OA\\MyProcess.zip")))).deploy();
        //获取流程定义
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();
        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("employee", application.getEmpId());
        Emp emp = empDAO.getEmpById(application.getEmpId());
        System.out.println(emp.getDept().getEmp().getId());
        variables.put("leader", emp.getDept().getEmp().getId());
        variables.put("staffing", emp.getDept().getEmp().getId());
//        variables.put("leader", 1);
//        variables.put("staffing",2);
        ProcessInstance pi = runtimeService.startProcessInstanceByKey(processDefinition.getKey(), variables);
        Task task = taskService.createTaskQuery().processInstanceId(pi.getId()).singleResult();
        taskService.complete(task.getId());

    }

    /**
     * 通过员工ID查询历史申请记录
     *
     * @param empid
     */
    @Override
    public List<Application> getById(String empid) {
        return applicationDAO.getById(empid);
    }

    /**
     * 修改申请状态
     *
     * @param status
     * @param id
     */
    @Override
    public void updateStatus(String processId, String status, String id) {
        Task task = taskService.createTaskQuery().processInstanceId(processId).singleResult();
        taskService.complete(task.getId());
        applicationDAO.updateStatus(status, id);
    }

    /**
     * 得到待办事务
     *
     * @param empid
     * @return
     */
    @Override
    public List<Task> getBusiness(String empid) {
        List<Task> list = processEngine.getTaskService()// 与正在执行的任务管理相关的Service
                .createTaskQuery()// 创建任务查询对象
                /** 查询条件（where部分） */
                .taskCandidateOrAssigned(empid)// 组任务的办理人查询
                /** 排序 */
                .orderByTaskCreateTime().asc()// 使用创建时间的升序排列
                /** 返回结果集 */
                .list();// 返回列表
        return list;
    }
}
